/*
 *  Find the top M elements from input
 *  One application of MinPQ
 */
package algs4;

import algs4.ds.MinPQ;

import java.util.Random;

public class TopM
{
    /* Main Test */
    public static void main(String[] args) {
        // check argument
        if (args.length != 1) {
            System.out.println("Usage: java TopM m");
            System.exit(1);
        }

        int m = Integer.parseInt(args[0]);

        Integer[] a = new Integer[25];
        Random    r = new Random(System.currentTimeMillis());

        // generate test data
        for (int i = 0; i < 25; i++) a[i] = r.nextInt(100);

        // show data
        for (int i = 0; i < 25; i++) System.out.printf("%2d ", a[i]);
        System.out.println();

        // create min priority queue
        MinPQ<Integer> pq = new MinPQ<>();

        for (int i = 0; i < 25; i++) {
            pq.insert(a[i]);

            if (pq.size() > m) pq.delMin();
        }

        // show data
        System.out.println("Top " + m + ":");
        for (Integer i : pq) System.out.printf("%2d ", i);
        System.out.println();
    }
}
